package com.chen.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.domain.Market;
import com.chen.dto.CoinDto;
import com.chen.fegin.CoinFegin;
import com.chen.mapper.MarketMapper;
import com.chen.service.MarketService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MarketServiceImpl extends ServiceImpl<MarketMapper, Market> implements MarketService{

    @Resource
    private CoinFegin coinFegin;

    @Override
    public void saveMarket(Market market) {
        List<Long> ids = new ArrayList<>();
        Long buyCoinId = market.getBuyCoinId();
        Long sellCoinId = market.getSellCoinId();
        ids.add(buyCoinId);
        ids.add(sellCoinId);
//        sellCoinId/buyCoinId
        Map<Long, CoinDto> coinByIds = coinFegin.getCoinByIds(ids);
        if(coinByIds.size()<2){
            throw new IllegalArgumentException("请先配置币种的信息");
        }

        CoinDto sellCoinDto = coinByIds.get(sellCoinId);
        CoinDto buyCoinDto = coinByIds.get(buyCoinId);

        market.setName(sellCoinDto.getName() + "/" + buyCoinDto.getName());
        market.setTitle(sellCoinDto.getName() + "/" + buyCoinDto.getName());
        market.setImg(sellCoinDto.getImg());
        market.setSymbol(sellCoinDto.getName() + buyCoinDto.getName());

        save(market);

    }
}
